Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
@thi.ng/compare
Advanced tools
Comparators with support for types implementing the @thi.ng/api/ICompare interface
@thi.ng/compare is a utility library for various comparison operations in JavaScript. It provides a collection of comparator functions for different data types and use cases, making it easier to perform sorting, equality checks, and other comparison-based operations.
Basic Comparators
Provides basic comparator functions for general use, including ascending and descending numerical comparisons.
const { compare, compareNumAsc, compareNumDesc } = require('@thi.ng/compare');
console.log(compare(5, 10)); // -1
console.log(compareNumAsc(5, 10)); // -1
console.log(compareNumDesc(5, 10)); // 1
String Comparators
Includes specialized comparators for string values, with options for case-sensitive and case-insensitive comparisons.
const { compareStr, compareStrCaseInsensitive } = require('@thi.ng/compare');
console.log(compareStr('apple', 'banana')); // -1
console.log(compareStrCaseInsensitive('Apple', 'apple')); // 0
Array Comparators
Allows for comparison of arrays, element by element, to determine their order or equality.
const { compareArrays } = require('@thi.ng/compare');
console.log(compareArrays([1, 2, 3], [1, 2, 4])); // -1
console.log(compareArrays([1, 2, 3], [1, 2, 3])); // 0
Object Comparators
Provides comparators for objects based on specific keys, useful for sorting arrays of objects.
const { compareByKey } = require('@thi.ng/compare');
const obj1 = { id: 1, name: 'Alice' };
const obj2 = { id: 2, name: 'Bob' };
console.log(compareByKey('id')(obj1, obj2)); // -1
Lodash is a popular utility library that provides a wide range of functions for common programming tasks, including comparison operations. It offers functions like _.isEqual for deep equality checks and _.sortBy for sorting collections. Compared to @thi.ng/compare, Lodash is more comprehensive but also larger in size.
fast-deep-equal is a small and fast library for deep equality checks. It is highly optimized for performance and is often used when deep comparison of objects or arrays is needed. Unlike @thi.ng/compare, it focuses solely on deep equality and does not provide other comparison utilities.
Ramda is a functional programming library for JavaScript that includes a variety of utility functions, including comparison functions like R.equals and R.sort. It emphasizes immutability and functional programming principles. While it offers similar comparison functionalities, it is part of a broader functional programming toolkit.
[!NOTE] This is one of 199 standalone projects, maintained as part of the @thi.ng/umbrella monorepo and anti-framework.
🚀 Please help me to work full-time on these projects by sponsoring me on GitHub. Thank you! ❤️
Comparators with optional support for types implementing the @thi.ng/api
ICompare
interface.
compareByKey()
compareByKeys2()
compareByKeys3()
compareByKeys4()
compareLengthAsc()
compareLengthDesc()
compareLex()
compareNumAsc()
compareNumDesc()
reverse()
STABLE - used in production
Search or submit any issues for this package
yarn add @thi.ng/compare
ESM import:
import * as cmp from "@thi.ng/compare";
Browser ESM import:
<script type="module" src="https://esm.run/@thi.ng/compare"></script>
For Node.js REPL:
const cmp = await import("@thi.ng/compare");
Package sizes (brotli'd, pre-treeshake): ESM: 804 bytes
Note: @thi.ng/api is in most cases a type-only import (not used at runtime)
Five projects in this repo's /examples directory are using this package:
Screenshot | Description | Live demo | Source |
---|---|---|---|
Color palette generation via dominant color extraction from uploaded images | Demo | Source | |
Piechart visualization of CSV data | Demo | Source | |
Full umbrella repo doc string search w/ paginated results | Demo | Source | |
Tree-based UI to find & explore thi.ng projects via their associated keywords | Demo | Source | |
Triple store query results & sortable table | Demo | Source |
import { ICompare } from "@thi.ng/api";
import { compare } from "@thi.ng/compare";
class Foo implements ICompare<Foo> {
x: number;
constructor(x: number) {
this.x = x;
}
compare(o: Foo) {
return compare(this.x, o.x);
}
}
compare(new Foo(1), new Foo(2));
// -1
Key-based object comparison is supported for 1 - 4 keys / dimensions.
import * as cmp from "@thi.ng/compare";
const src = [
{ id: "charlie", age: 66 },
{ id: "bart", age: 42 },
{ id: "alice", age: 23 },
{ id: "dora", age: 11 },
];
// cluster sort by id -> age (default comparators)
console.log(
[...src].sort(cmp.compareByKeys2("id", "age"))
);
// [
// { id: 'alice', age: 23 },
// { id: 'bart', age: 42 },
// { id: 'charlie', age: 66 },
// { id: 'dora', age: 11 }
// ]
// cluster sort by age -> id (default comparators)
console.log(
[...src].sort(cmp.compareByKeys2("age", "id"))
);
// [
// { id: 'dora', age: 11 },
// { id: 'alice', age: 23 },
// { id: 'bart', age: 42 },
// { id: 'charlie', age: 66 }
// ]
// cluster sort by age -> id
// (custom comparator for `age` key)
console.log(
[...src].sort(cmp.compareByKeys2("age", "id", cmp.compareNumDesc))
);
// [
// { id: 'charlie', age: 66 },
// { id: 'bart', age: 42 },
// { id: 'alice', age: 23 },
// { id: 'dora', age: 11 }
// ]
// using `reverse()` comparator for `id`
console.log(
[...src].sort(cmp.compareByKeys2("age", "id", cmp.compare, cmp.reverse(cmp.compare)))
);
// [
// { id: 'dora', age: 11 },
// { id: 'alice', age: 23 },
// { id: 'bart', age: 42 },
// { id: 'charlie', age: 66 }
// ]
If this project contributes to an academic publication, please cite it as:
@misc{thing-compare,
title = "@thi.ng/compare",
author = "Karsten Schmidt",
note = "https://thi.ng/compare",
year = 2016
}
© 2016 - 2024 Karsten Schmidt // Apache License 2.0
FAQs
Comparators with support for types implementing the @thi.ng/api/ICompare interface
The npm package @thi.ng/compare receives a total of 90,538 weekly downloads. As such, @thi.ng/compare popularity was classified as popular.
We found that @thi.ng/compare demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.